<?php
/**
 * 日志管理
 */
namespace app\htsoft\controller;
use think\Db;
class Log extends \app\common\controller\Base {
	
    function _initialize() {
        parent::_initialize();
    }
	/**
	 * 登录日志
	 */
	public function login()
	{
		$nowpage = input('page',1, 'intval');
		$limits = input('limits',10, 'intval');
		$_GET = input();
		$map=array();
		$start = input('start');
		$end = input('end');
		if($start){
    		$map['log_time'] = ['EGT',$start];
    		if($end){
    			$map['log_time'] = ['between', [$start,$end]];
    		}
    	}else if($end){
    		$map['log_time'] = ['ELT',$end];
    	}
    	$user_login = input('user_login');
    	if($user_login){
    		$map['u.user_login|u.nicename']=array("like","%".$user_login."%");
    	}
    	$ids = Db::name("admin_log") -> where(["log_time"=>["lt",time()-60*60*24*config('delete_days')]]) -> column("log_id");
    	if($ids) Db::name("admin_log") -> where(["log_id"=>["in",$ids]]) -> update(["delete"=>1]);
		$map["a.log_type"] = 0;
		$map["a.delete"] = 0;
		$join1 = " u.id = a.admin_id";
		// 获取总条数
		$count = Db::name('admin_log') ->alias("a") ->join(DB_PREFIX."admin_users u",$join1) ->where($map) -> count();
		// 计算总页面
		$allpage = ceil($count / $limits);
		$allpage = intval($allpage); if($nowpage > $allpage){$nowpage = $allpage;}
		$msg = Db::name('admin_log') ->alias("a") ->join(DB_PREFIX."admin_users u",$join1) ->where($map)
                ->field("a.*,u.user_login,u.nicename,u.id") -> page($nowpage, $limits)->order("a.log_id desc")->select ();

		$this -> assign ( "msg", $msg );
		$this -> assign('allpage', $allpage);
		$this -> assign('nowpage', $nowpage);
		$this -> assign('allcount', $count);
		$this -> assign('limits', $limits);
		$this -> assign('formget', $_GET);
		return $this -> fetch();
	}
    /**
     * 操作日志
     */
    public function operation()
    {
        $nowpage = input('page',1, 'intval');
        $limits = input('limits',10, 'intval');
        $_GET = input();
        $map=array();
        $start = input('start');
        $end = input('end');
        $user_login = input('user_login');
        $title = input('title');
        if($user_login){
            $map['u.user_login|u.nicename']=array("like","%".$user_login."%");
        }
        if($title){
            $map['a.log_info']=array("like","%".$title."%");
        }
        if($start){
            $map['log_time'] = ['EGT',$start];
            if($end){
                $map['log_time'] = ['between', [$start,$end]];
            }
        }else if($end){
            $map['log_time'] = ['ELT',$end];
        }
        $ids = Db::name("admin_log") -> where(["log_time"=>["lt",time()-60*60*24*config('delete_days')]]) -> column("log_id");
        if($ids) Db::name("admin_log") -> where(["log_id"=>["in",$ids]]) -> update(["delete"=>1]);
        $map["a.delete"] = 0;
        $map["log_type"] = 1;
        $join1 = "u.id = a.admin_id";
        // 获取总条数
        $count = Db::name('admin_log') ->alias("a") ->join(DB_PREFIX."admin_users u",$join1) ->where($map) -> count();
        // 计算总页面
        $allpage = ceil($count / $limits);
        $allpage = intval($allpage); if($nowpage > $allpage){$nowpage = $allpage;}
        $msg = Db::name('admin_log') ->alias("a") ->join(DB_PREFIX."admin_users u",$join1) ->where($map)
            ->field("a.*,u.user_login,u.nicename,u.id")-> page($nowpage, $limits)->order("a.log_id desc")->select ();
        $this -> assign ( "msg", $msg );
        $this -> assign('allpage', $allpage);
        $this -> assign('nowpage', $nowpage);
        $this -> assign('allcount', $count);
        $this -> assign('limits', $limits);
        $this -> assign('formget', $_GET);
        return $this -> fetch();
    }
	 
	
}